﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using Ninject;
using Dapper;

namespace VideoChat.Models
{
    public class User : IUser
    {
        public string Key { get; set; }
        public string Name { get; set; }

        public User() { }

        public User(string key, string name)
        {
            Key = key;
            Name = name;
        }
    }

    public class CahceUsersStore : IUsersStore
    {
        private static IList<IUser> _users;

        private static readonly IDictionary<string, RoomModel> _roomInfo = new Dictionary<string, RoomModel>();

        private IList<IUser> GetStore()
        {
            return _users ?? (_users = new List<IUser>());
        }

        private void SaveStore(IList<IUser> store)
        {
            _users = store;
        }

        public bool Exists(string key)
        {
            var store = GetStore();
            return store.Any(u => u.Key == key);
        }

        public void Add(IUser user)
        {
            if (!Exists(user.Key))
            {
                var store = GetStore();
                store.Add(user);
                SaveStore(store);
            }
        }

        public void Remove(string key)
        {
            var store = GetStore();
            if (store.Any(u => u.Key == key))
            {
                var user = store.FirstOrDefault(u => u.Key == key);
                if (user != null)
                    store.Remove(user);
            }
            SaveStore(store);
        }

        public IList<IUser> GetList()
        {
            return GetStore();
        }

        public bool CheckFlooder(string key)
        {
            return false;
        }

        public void SaveMessage(ChatMessage message)
        {
            //Not implemented
        }

        public void SaveRoomInfo(RoomModel model)
        {
            _roomInfo.Add(model.RoomKey, model);
        }

        public RoomModel GetRoomInfo(string key)
        {
            return _roomInfo[key];
        }

        public void CloseRoom(string chatKey)
        {
            //Not implemented
        }

        public IList<ChatMessage> GetChatHistory(string roomKey)
        {
            //Not implemented
            return new List<ChatMessage>();
        }
        public void CloseRoom(string roomKey, string userKey, VideoState state)
        {
            // Not implemented
        }
        public void SaveRoomState(string roomKey, string key, bool isCam, bool isCamOn, bool isMic, bool isMicOn)
        {
            //Not implemented
        }
    }

    public class Data
    {
        private readonly IKernel _ninjectKernel;

        public Data()
        {
            _ninjectKernel = new StandardKernel();
            AddBindings();
        }

        private void AddBindings()
        {
            _ninjectKernel.Bind<IUser>().To<User>();
            _ninjectKernel.Bind<IUsersStore>().To<DataBaseUsersStore>();
        }


        public IUsersStore GetUsersStore()
        {
            return _ninjectKernel.Get<IUsersStore>();
        }

    }
}